import Mock, { Random } from "mockjs";
// 创建数据集 type object
const dataList = Mock.mock({
  "items|15": [
    {
      id: "@id()",
      name: "@ctitle(2,4)Java",
      state: "未完成",
      upDate: "@datetime",
      builtDate: "@datetime",
    },
  ],
});
const items = dataList.items;
// 模拟增删改查
// 获取数据
export function getData(options) {
  const rtype = options.type.toLowerCase();
  // console.log('接口拿到的数据',options)
  // console.log('接口拿到的数据转为json',JSON.parse(options.body) )
  // console.log('接口拿到的数据转为json',JSON.parse(options.body).pageSize )

  if (rtype === "get") {
    // 选取指定页数的数据 把传过来的json格式参数转为js对象 然后解构赋值
    const { pageSize, currentPage } = JSON.parse(options.body);

    // 只选返回当前页部分数据

    const returnList = items.filter(
      (item, index) =>
        index < pageSize * currentPage && index >= pageSize * (currentPage - 1)
    );
    // console.log("相乘", pageSize * currentPage);
    return {
      currentPageData: returnList,
      totalData: dataList.items.length,
      messge: "获取成功！",
    };
  } else {
    return { messge: "获取失败，请求方法错误！" };
  }
}
// 增
export function addData(options) {
  const rtype = options.type.toLowerCase();
  switch(rtype){
    case 'get':
      break;
    case 'post':
      const newForm = JSON.parse(options.body).addForm;
      // 给newForm 生成一个id
      console.log('ddd',newForm)
      newForm.id = Math.round(Math.random()*10000000)
      items.push(newForm)
  }
  return {messge:'添加成功'};
}
// 编辑条目
export function editData(options) {
  // 判断请求类型，符合类型才启动接口
  const rtype = options.type.toLowerCase();
  switch(rtype){
    case 'get':
      break;
    case 'post':
      const editForm = JSON.parse(options.body).editForm;
      console.log(editForm)
      const editItem = items.filter((item,index)=>{
        if(editForm.id===item.id){
          const currObj = items[index];
          for(let key in currObj){
            currObj[key] = editForm[key];
          };
        }
        })

  }
  return {messge:'编辑成功'};
}

// 删
export function deleteData(options) {
  // 判断请求类型，符合类型才启动接口
  const rtype = options.type.toLowerCase();
  switch (rtype) {
    case "get":
      break;
    case "post":
      const deleteId = JSON.parse(options.body).deleteId;
      // 删除数据后用getData拿到剩余数据
      let deleteIndex = -1;
      const deleteItem = items.filter((item, index) => {
        if (item.id === deleteId) {
          deleteIndex = index;
          return true;
        } else {
          return false;
        }
      });
      items.splice(deleteIndex, 1);
      break;
  }
  return { messge: "删除成功！" };
}
// 关键字查询
export function searchData(options) {
  const rtype = options.type.toLowerCase();
  // console.log('接口拿到的数据',options)
  // console.log('接口拿到的数据转为json',JSON.parse(options.body) )
  // console.log('接口拿到的数据转为json',JSON.parse(options.body).pageSize )

  if (rtype === "post") {
    // 获取查询关键字
    const {searchKey,pageSize, currentPage} = JSON.parse(options.body)
    // 只选返回当前页部分数据
    console.log('关键字',searchKey)
    const resList = items.filter((item,index)=>{
      if(searchKey && item.name.indexOf(searchKey)===-1){return false} else{return true}
      
    })
    const returnList = resList.filter(
      (item, index) =>
        index < pageSize * currentPage && index >= pageSize * (currentPage - 1)
    );
    // console.log("相乘", pageSize * currentPage);
    return {
      currentPageData: returnList,
      totalData: resList.length,
      messge: "获取成功！",
    };
  } else {
    return { messge: "获取失败，请求方法错误！" };
  }
}